package com.e3.sysmg.config.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.core.lang.DataType;
import com.e3.sysmg.config.model.ParameterConstant;
import com.e3.sysmg.config.model.ParameterModel;
import com.e3.sysmg.config.model.ParameterTreeModel;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.e3.web.widget.client.MyGridHelper;
import com.e3.web.widget.model.ListTypeModel;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class ParameterHelper extends GFHelper<ParameterModel>{
	
	public static ParameterHelper INSTANCE = new ParameterHelper();
	
	private String[] notNullFields = {ParameterModel.NAME,ParameterModel.TYPE,ParameterModel.DATATYPE,ParameterModel.LEVEL};
	
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private ParameterHelper(){
		lengthMap.put(ParameterModel.ID, 19);
		lengthMap.put(ParameterModel.NAME, 50);
		lengthMap.put(ParameterModel.TYPE, 2);
		lengthMap.put(ParameterModel.DATATYPE, 2);
		lengthMap.put(ParameterModel.VALUE, 50);
		lengthMap.put(ParameterModel.LEVEL, 2);
		lengthMap.put(ParameterModel.REMARK, 100);
	}

	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		int length = lengthMap.get(name);
		if(name.equals(ParameterModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}
		else if(name.equals(ParameterModel.NAME)||name.equals(ParameterModel.VALUE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,length,isNotNull);
		}
		else if(name.equals(ParameterModel.TYPE)){
			formField = MyFormFieldFactory.createListField(name, dispName,getTypeListTypeModel() , true,isNotNull);
		}
		else if(name.equals(ParameterModel.DATATYPE)){
			formField = MyFormFieldFactory.createListField(name, dispName,getDataTypeListTypeModel() , true,isNotNull);
		}
		else if(name.equals(ParameterModel.LEVEL)){
			formField = MyFormFieldFactory.createListField(name, dispName,getLevelListTypeModel() , true,isNotNull);
		}
		else if(name.equals(ParameterModel.REMARK)){
			formField = MyFormFieldFactory.createTextAreaField(name, dispName,length,isNotNull);
		}
		
		return formField;
	}
	
	private List<ListTypeModel<Integer>> getTypeListTypeModel(){
		List<ListTypeModel<Integer>> values = new ArrayList<ListTypeModel<Integer>>();
		Integer code = ParameterConstant.TYPE_COMMON;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.TYPE_COMMON));
		values.add(ltModel);
		
		code = ParameterConstant.TYPE_PURCHASE;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.TYPE_PURCHASE));
		values.add(ltModel);
		
		code = ParameterConstant.TYPE_SALES;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.TYPE_SALES));
		values.add(ltModel);
		
		code = ParameterConstant.TYPE_INVENTORY;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.TYPE_INVENTORY));
		values.add(ltModel);
		
		code = ParameterConstant.TYPE_ARAP;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.TYPE_ARAP));
		values.add(ltModel);
		
		return values;
	}
	
	private List<ListTypeModel<Integer>> getLevelListTypeModel(){
		List<ListTypeModel<Integer>> values = new ArrayList<ListTypeModel<Integer>>();
		Integer code = ParameterConstant.LEVEL_PROGRAMMER;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.LEVEL_PROGRAMMER_NAME));
		values.add(ltModel);
		
		code = ParameterConstant.LEVEL_ADMINISTRATOR;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.LEVEL_ADMINISTRATOR_NAME));
		values.add(ltModel);
		
		code = ParameterConstant.LEVEL_OPERATOR;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.LEVEL_OPERATOR_NAME));
		values.add(ltModel);
	
		return values;
	}
	
	private List<ListTypeModel<Integer>> getDataTypeListTypeModel(){
		List<ListTypeModel<Integer>> values = new ArrayList<ListTypeModel<Integer>>();
		Integer code = DataType.STRING;
		ListTypeModel<Integer> ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_STRING_NAME));
		values.add(ltModel);
		
		code = DataType.INT;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_INT_NAME));
		values.add(ltModel);
		
		code = DataType.DOUBLE;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_DOUBLE_NAME));
		values.add(ltModel);
		
		code = DataType.LIST;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_LIST_NAME));
		values.add(ltModel);
		
		code = DataType.DATE;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_DATE_NAME));
		values.add(ltModel);
		
		code = DataType.BOOLEAN;
		ltModel = new ListTypeModel<Integer>(code, getFieldDisplayName(ParameterModel.DATATYPE_BOOLEAN_NAME));
		values.add(ltModel);
	
		return values;
	} 
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = ParameterModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    Object[] typeCodes = {ParameterConstant.TYPE_COMMON,ParameterConstant.TYPE_PURCHASE,ParameterConstant.TYPE_SALES,ParameterConstant.TYPE_INVENTORY,ParameterConstant.TYPE_ARAP};
		String s1 = getFieldDisplayName(ParameterModel.TYPE_COMMON);
		String s2 = getFieldDisplayName(ParameterModel.TYPE_PURCHASE);
		String s3 = getFieldDisplayName(ParameterModel.TYPE_SALES);
		String s4 = getFieldDisplayName(ParameterModel.TYPE_INVENTORY);
		String s5 = getFieldDisplayName(ParameterModel.TYPE_ARAP);
		String[] typeNames = {s1,s2,s3,s4,s5};
		GridCellRenderer<ParameterModel> typeRenderer = MyGridHelper.createListCellRender(ParameterModel.TYPE, typeCodes, typeNames);
		column = new ColumnConfig();
		name = ParameterModel.TYPE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		column.setRenderer(typeRenderer);
		configs.add(column);
		
		Object[] levelCodes = {ParameterConstant.LEVEL_PROGRAMMER,ParameterConstant.LEVEL_ADMINISTRATOR,ParameterConstant.LEVEL_OPERATOR};
		String l1 = getFieldDisplayName(ParameterModel.LEVEL_PROGRAMMER_NAME);
		String l2 = getFieldDisplayName(ParameterModel.LEVEL_ADMINISTRATOR_NAME);
		String l3 = getFieldDisplayName(ParameterModel.LEVEL_OPERATOR_NAME);
		String[] levelNames = {l1,l2,l3};
		GridCellRenderer<ParameterModel> levelRenderer = MyGridHelper.createListCellRender(ParameterModel.LEVEL, levelCodes, levelNames);
		column = new ColumnConfig();
		name = ParameterModel.LEVEL;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		column.setRenderer(levelRenderer);
		configs.add(column);
		
		Object[] dataCodes = {DataType.STRING,DataType.INT,DataType.DOUBLE,DataType.LIST,DataType.DATE,DataType.BOOLEAN};
		String d1 = getFieldDisplayName(ParameterModel.DATATYPE_STRING_NAME);
		String d2 = getFieldDisplayName(ParameterModel.DATATYPE_INT_NAME);
		String d3 = getFieldDisplayName(ParameterModel.DATATYPE_DOUBLE_NAME);
		String d4 = getFieldDisplayName(ParameterModel.DATATYPE_LIST_NAME);
		String d5 = getFieldDisplayName(ParameterModel.DATATYPE_DATE_NAME);
		String d6 = getFieldDisplayName(ParameterModel.DATATYPE_BOOLEAN_NAME);
		String[] dataNames = {d1,d2,d3,d4,d5,d6};
		GridCellRenderer<ParameterModel> dataRenderer = MyGridHelper.createListCellRender(ParameterModel.DATATYPE, dataCodes, dataNames);
		column = new ColumnConfig();
		name = ParameterModel.DATATYPE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		column.setRenderer(dataRenderer);
		configs.add(column);
		
		column = new ColumnConfig();
		name = ParameterModel.VALUE;
		column.setId(name);
		column.setHeader(getFieldDisplayName(name));
		column.setWidth(200);
		configs.add(column);
	    
	    return configs;
	}
	
	public List<ParameterTreeModel> getParameterTreeRootModel(){
		List<ParameterTreeModel> data = new ArrayList<ParameterTreeModel>(1);
		
		ParameterTreeModel rootModel = new ParameterTreeModel();
		rootModel.setCode(0);
		//rootModel.setName(ParameterMessages.INSTANCE.tree_root_name());
		rootModel.setHasChildren(true);
		data.add(rootModel);
				
		return data;
	}
	
	public List<ParameterTreeModel> getParameterTreeData(){
		List<ParameterTreeModel> children = new ArrayList<ParameterTreeModel>(4);
		ParameterTreeModel model = new ParameterTreeModel();
		model.setCode(ParameterConstant.TYPE_COMMON);
		//model.setName(getFieldDisplayName(ParameterModel.TYPE_COMMON));
		model.setHasChildren(false);
		model.setId(10000000001L);
		children.add(model);
		
		model = new ParameterTreeModel();
		model.setCode(ParameterConstant.TYPE_PURCHASE);
		//model.setName(getFieldDisplayName(ParameterModel.TYPE_PURCHASE));
		model.setHasChildren(false);
		model.setId(10000000002L);
		children.add(model);
		
		model = new ParameterTreeModel();
		model.setCode(ParameterConstant.TYPE_SALES);
		//model.setName(getFieldDisplayName(ParameterModel.TYPE_SALES));
		model.setHasChildren(false);
		model.setId(10000000003L);
		children.add(model);
		
		model = new ParameterTreeModel();
		model.setCode(ParameterConstant.TYPE_INVENTORY);
		//model.setName(getFieldDisplayName(ParameterModel.TYPE_INVENTORY));
		model.setHasChildren(false);
		model.setId(10000000004L);
		children.add(model);
		
		model = new ParameterTreeModel();
		model.setCode(ParameterConstant.TYPE_ARAP);
		//model.setName(getFieldDisplayName(ParameterModel.TYPE_ARAP));
		model.setHasChildren(false);
		model.setId(10000000005L);
		children.add(model);

		return children;
	}
	
	public String getFieldDisplayName(String code) {
		return ParameterFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public ParameterModel createModelInstance() {
		return new ParameterModel();
	}

}
